import 'dart:math';
import 'package:intl/intl.dart' show DateFormat;
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yi_shop/model/lucky.dart';
import 'package:yi_shop/ui/scroll_refresh_config.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/tools.dart';

class LuckyRecordController extends GetxController {
  RxList<LuckyRecordModel> pageList = RxList([]);

  RefreshController refreshController = RefreshController();

  @override
  void onInit() {
    initData();
    super.onInit();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 42) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    initData();

    /// 完毕
    refreshController.loadComplete();
  }

  void initData() {
    var r = Random.secure();
    for (var i = 0; i < 20; i++) {
      DateTime now = DateTime.now();
      DateTime date = now.subtract(Duration(days: r.nextInt(100)));
      pageList.add(LuckyRecordModel(
          winTime: DateFormat('yyyy-MM-dd HH:mm').format(date), amount: r.nextInt(100)));
    }
  }
}

class LuckyRecordPage extends GetView<LuckyRecordController> {
  const LuckyRecordPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: Text(
          '抽奖记录',
          style: StylesUtils.customTextStyle(
              fontSize: StylesUtils.fontSize18, fontWeight: FontWeight.w700),
        ),
        bottom: PreferredSize(
          preferredSize: const Size.fromHeight(40),
          child: Container(
            height: 40,
            color: Colors.white,
            child: Row(
              children: [
                Expanded(
                    child: Center(
                  child: Text(
                    '中将日期',
                    style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                  ),
                )),
                Expanded(
                    child: Center(
                  child: Text(
                    '中将内容',
                    style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                  ),
                )),
              ],
            ),
          ),
        ),
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (_, int index) {
        var row = controller.pageList[index];
        return Container(
          height: 40,
          decoration: const BoxDecoration(color: Colors.white),
          child: Row(
            children: [
              Expanded(
                  child: Center(
                child: Text(
                  row.winTime,
                  style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                ),
              )),
              Expanded(
                  child: Center(
                child: Text(
                  '${row.amount}元优惠券',
                  style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                ),
              )),
            ],
          ),
        );
      },
      itemCount: controller.pageList.length,
      itemExtent: 40,
    );
  }
}
